#include "../../common/mysql_message.hpp"
#include "../../common/utils.hpp"

std::shared_ptr<odb::core::database> db;
MessageTable::ptr msg_table;
void test_insert(){
    unsigned long time = 1756039436;
    boost::posix_time::ptime create_time1 = boost::posix_time::from_time_t(time);
    time -= 100;
    boost::posix_time::ptime create_time2 = boost::posix_time::from_time_t(time);


    std::string session_id = uuid();
    Message msg1(uuid(), session_id, "6919-9f03ea75-0000", 0, create_time1);
    std::string msg_info = "roboko2, 你好";
    msg1.set_content(msg_info);
    msg1.set_file_size(msg_info.size());


    Message msg2(uuid(), session_id, "e7b5-538ed478-0001", 0, create_time2);
    std::string msg_info2 = "roboko1, 你好";
    msg2.set_content(msg_info2);
    msg2.set_file_size(msg_info2.size());


    msg_table->insert(msg1);
    msg_table->insert(msg2);
}

void insert2() {
    Message m1("消息ID1", "会话ID1", "用户ID1", 0, boost::posix_time::time_from_string("2002-01-20 23:59:59.000"));
    std::string msg_info1 = "消息内容1";
    m1.set_content(msg_info1);
    m1.set_file_size(msg_info1.size());

    Message m2("消息ID2", "会话ID2", "用户ID2", 0, boost::posix_time::time_from_string("2002-01-21 00:00:00.000"));
    std::string msg_info2 = "消息内容2";
    m2.set_content(msg_info2);
    m2.set_file_size(msg_info2.size());
    Message m3("消息ID3", "会话ID3", "用户ID3", 0, boost::posix_time::time_from_string("2002-01-21 00:00:01.000")); 
    std::string msg_info3 = "消息内容3";
    m3.set_content(msg_info3);
    m3.set_file_size(msg_info3.size());
    Message m4("消息ID4", "会话ID4", "用户ID4", 0, boost::posix_time::time_from_string("2002-01-21 00:00:02.000"));
    std::string msg_info4 = "消息内容4";
    m4.set_content(msg_info4);
    m4.set_file_size(msg_info4.size());

    Message m5("消息ID5", "会话ID4", "用户ID5", 0, boost::posix_time::time_from_string("2002-01-21 00:00:03.000"));
    std::string msg_info5 = "消息内容5";
    m5.set_content(msg_info5);
    m5.set_file_size(msg_info5.size());

    msg_table->insert(m1);
    msg_table->insert(m2);
    msg_table->insert(m3);
    msg_table->insert(m4);
    msg_table->insert(m5);
}

void remove_test() {
    msg_table->remove("会话ID4");
}

void range_test() {
    auto res = msg_table->range("会话ID1", 
                     boost::posix_time::time_from_string("2002-01-20 23:59:59"),
                     boost::posix_time::time_from_string("2002-01-21 00:00:01"));
    for (const auto& msg : res) {
        LOG_INFO("消息ID: {}, 会话ID: {}, 内容: {}, 创建时间: {}",
                 msg.message_id(), msg.session_id(), msg.content(), boost::posix_time::to_simple_string(msg.create_time()));
    }
}

void recent_test() {
    auto res = msg_table->recent("会话ID1", 4);
    for (const auto& msg : res) {
        LOG_INFO("消息ID: {}, 会话ID: {}, 内容: {}, 创建时间: {}",
                 msg.message_id(), msg.session_id(), msg.content(), boost::posix_time::to_simple_string(msg.create_time()));
    }
}
int main() {
    init_logger();
    db = ODBFactory::create(
        "root",
        "&a==nullPTR1",
        "chat_system"
    );
    msg_table = std::make_shared<MessageTable>(db);
    // test_insert();
    // insert2();
    // remove_test();
    // range_test();
    recent_test();
    return 0;
}
